from app.services.reques_http import RequestHandler
from flask import Blueprint
from app import app

import json
from common import CentralPeoplesGovernmentCommon
from flask.json import jsonify
from app.services.JSONResponse import JSONResponse
from flask import request
from app.services.paramRequiredError import Params

central_peoples_governmenet_bp = Blueprint(
    'central_peoples_governmenet_bp', __name__, url_prefix='/' + CentralPeoplesGovernmentCommon().central_peoples_government)


@central_peoples_governmenet_bp.route('/' + CentralPeoplesGovernmentCommon().get_many, methods=['POST'])
def get_many():
    _from = request.args.get("from")
    _size = request.args.get("size")
    print(_from)
    # return "get math"
    # get请求接口
    url = CentralPeoplesGovernmentCommon().fuwuqiUrl + "/_search"
    # body请求的body
    payload = {
        "from": _from,
        "size": _size,

    }
    res = RequestHandler().get(url=url, params=payload)
    print(res)
    newRes = res.json()['hits']['hits']
    list = []
    for item in newRes:
        list.append(item['_source'])
    return JSONResponse(list)


@central_peoples_governmenet_bp.route('/' + CentralPeoplesGovernmentCommon().get_one, methods=['POST'])
def get_one():
    # return "get math"
    # get请求接口
    key = request.get_json()
    # print(range)
    url = CentralPeoplesGovernmentCommon().fuwuqiUrl + "/_doc" + \
        "/" + key['range']
    print("url", url)
    # body请求的body
    res = RequestHandler().get(url=url)
    print(res)
    newRes = res.json()
    if newRes['found'] == False:
        return {"found": "false"}
    else:
        # list = []
        # for item in newRes:
        #     list.append(item['_source'])
        return newRes['_source']
